﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MKAdminCore.Controllers;
using MKAdminCore.DTO.CusAttribute;
using MKAdminCore.DTO.Web.Common;
using MKAdminCore.DTO.Web.Communicate;
using MKAdminCore.ToolKit.Config;
using MKAdminCore.ToolKit.RegexHelper;

namespace MKAdminCore.Areas.Communicate.Controllers
{
    [Area("Communicate")]
    [Route("Communicate/[controller]/[action]")]
    public class RegexController : BaseMvcController
    {
        public IActionResult Index()
        {
            return View(PageViewFilesConfig.CommunicateRegexIndex);
        }

        /// <summary>
        /// 验证内容是否匹配
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult IsMatch(RegexIsMatchParam parameter)
        {
            var result = new HttpResponseResult() { Msg = "匹配" };
            var validateResult = parameter.Validate();
            if (validateResult.Code != 200)
            {
                result.Msg = validateResult.Msg;
                result.Code = validateResult.Code;
                return Ok(result);
            }

            parameter.TextContent = parameter.TextContent.Trim();
            var r_content = new Regex(parameter.RegexPattern, RegexOptions.IgnoreCase);
            Match m_content = r_content.Match(parameter.TextContent);

            if (m_content.Success && m_content.Value == parameter.TextContent)
            {
                result.Code = 200;
            }
            else
            {
                result.Msg = "不匹配";
            }

            return Ok(result);
        }

        [HttpPost]
        public IActionResult GetMatchPattern(GetMatchPatternParam parameter)
        {
            var result = new HttpResponseResult<string>() { Msg = "" };
            var validateResult = parameter.Validate();
            if (validateResult.Code != 200)
            {
                result.Msg = validateResult.Msg;
                result.Code = validateResult.Code;
                return Ok(result);
            }
            result.Data = RegexMatchPattern.GetMatchPattern(parameter.RegexPattern);

            return Ok(result);
        }
    }
}